
cap log close
clear
clear matrix
clear mata
pause on
set more off
set mem 700m
set maxvar 10000

/*
Name: BMJRR_treattables.do
Date Created: March 9, 2010
Description: Creates tables for endline and logbook treatment effects for both spillover and primary villages. Note: this will output csv files with the numbers for the tables.
*/

local date = c(current_date)
log using "BMJRR_treattables_xtmepoisson `date'.log", replace


/*
***
***Endline Analysis
***
*/

use "imm_mergedwHHandBaseline.dta", replace

**Create scar indicators
gen scar = 1 if d3_16==1|d3_16==2
replace scar = 0 if d3_16==3

gen scar1to3 = scar if agemon>=12 & agemon<=36

**Generate a variable for complete conditional on having 1 immunization
gen complete1to3_if1 = (complete1to3==1) if (d2_5>0&d2_5<.)&(agemon>=12&agemon<=36)


***
***Non-Spillover Hamlets
***
gen rows = ""

replace rows = "Number of Immunizations" if _n==1
replace rows = "At Least One Immunization" if _n==3
replace rows = "Has BCG Scar" if _n==5
replace rows = "Completely Immunized" if _n==7
replace rows = "Completely Immunized (Subset With At Least 1 Immunization)" if _n==9

gen zztreat1=.
gen zztreat2=.
gen zztreat3=.
gen zzdtreat2=.
gen zzdtreat3=.
gen zzdtreat32=.

gen zzortreat2=.
gen zzortreat3=.
gen zzortreat32=.


local i=1

*dependent variables
local varlist "d2_5_1to3 atleast1shot_1to3 scar1to3 complete1to3 complete1to3_if1"

foreach var of local varlist {
	
	*treatment means
	foreach num of numlist 1(1)3 {
		reg `var' if treat`num'==1 & spillover==0, cluster(locid)
		replace zztreat`num'=_b[_cons] if _n==`i'
		replace zztreat`num'=_se[_cons] if _n==`i'+1
	}
	
	
	foreach num of numlist 2(1)3 {
		*random effects difference in means vs. control
		xtmixed `var' treat`num' if (treat`num'==1|treat1==1)&spillover==0 || hidno: || locid:, technique(dfp)
		replace zzdtreat`num'=_b[treat`num'] if _n==`i'
		replace zzdtreat`num'=_se[treat`num'] if _n==`i'+1

		*random effects adjusted relative risk vs. control
		xtmepoisson `var' treat`num' if (treat`num'==1|treat1==1)&spillover==0 || hidno: || locid:
		replace zzortreat`num'=exp(_b[treat`num']) if _n==`i'
		replace zzortreat`num'=_se[treat`num'] if _n==`i'+1

	}

	*random effects difference in means incentive vs. non-incentive
	xtmixed `var' treat3 if (treat3==1|treat2==1)&spillover==0 || hidno: || locid:, technique(dfp)
	replace zzdtreat32=_b[treat3] if _n==`i'
	replace zzdtreat32=_se[treat3] if _n==`i'+1

	*random effects adjusted relative risk incentive vs. non-incentive
	xtmepoisson `var' treat3 if (treat3==1|treat2==1)&spillover==0 || hidno: || locid:
	replace zzortreat32=exp(_b[treat3]) if _n==`i'
	replace zzortreat32=_se[treat3] if _n==`i'+1


	local i=`i'+2
}
outsheet rows zz* using BMJRR_treattables_1a_endlinenospill.csv, comma replace

*
*NOTE: rest of sections proceed with the same regressions for different subsamples,
*spillover and logbook samples
*


***
***Spillover Hamlets
***

drop zz*
gen zztreat1=.
gen zztreat2=.
gen zztreat3=.
gen zzdtreat2=.
gen zzdtreat3=.
gen zzdtreat32=.

gen zzortreat2=.
gen zzortreat3=.
gen zzortreat32=.


local i=1


local varlist "d2_5_1to3 atleast1shot_1to3 scar1to3 complete1to3 complete1to3_if1"

foreach var of local varlist {

reg `var' if treat1==1  , cluster(locid)
replace zztreat1=_b[_cons] if _n==`i'
replace zztreat1=_se[_cons] if _n==`i'+1

foreach num of numlist 2(1)3 {
reg `var' if treat`num'==1 & spillover==1 , cluster(locid)
replace zztreat`num'=_b[_cons] if _n==`i'
replace zztreat`num'=_se[_cons] if _n==`i'+1
}

foreach num of numlist 2(1)3 {

	xtmixed `var' treat`num' if (treat`num'==1&spillover==1)|treat1==1 || hidno: || locid:, technique(dfp)
	replace zzdtreat`num'=_b[treat`num'] if _n==`i'
	replace zzdtreat`num'=_se[treat`num'] if _n==`i'+1

	xtmepoisson `var' treat`num' if (treat`num'==1&spillover==1) | treat1==1 || hidno: || locid:
	replace zzortreat`num'=exp(_b[treat`num']) if _n==`i'
	replace zzortreat`num'=_se[treat`num'] if _n==`i'+1


}


xtmixed `var' treat3 if (treat3==1|treat2==1)&spillover==1 || hidno: || locid:, technique(dfp)
replace zzdtreat32=_b[treat3] if _n==`i'
replace zzdtreat32=_se[treat3] if _n==`i'+1

xtmepoisson `var' treat3 if (treat3==1|treat2==1)&spillover==1 || hidno: || locid:
replace zzortreat32=exp(_b[treat3]) if _n==`i'
replace zzortreat32=_se[treat3] if _n==`i'+1


local i=`i'+2
}

outsheet rows zz* using BMJRR_treattables_3a_endlinespill.csv, comma replace


/*
***
***Logbook Analysis
***
*/


tempfile treat
use "TreatmentStatus.dta", clear
keep locid immun_treat incent_treat

save `treat'

use "GNMLogbooks.dta", clear

joinby locid using `treat', unmatched(master)
*rename to fit previous regressions in this file, make treatment variables orthogonal (immun_treat is a subset of incent_treat in the data, not what we want for regression here).
rename incent_treat treat3
rename immun_treat treat2
replace treat2 = 0 if treat3==1

*list of all shot types in data
local shots "bcg dt1 dt2 dt3 measels dt_booster tt1 tt2"

*Count the number of immunizations each kid got at the camp and the number each kid received total (rec vs. imm)
foreach shot in `shots' {
	gen `shot'_rec = (`shot'_day>0&`shot'_day<32)
	gen `shot'_imm = (`shot'_day!=.)

}

*designate control and spillover in logbooks
gen control = (treat2==0&treat3==0)
drop if control==1

gen spillover=(out_in!=1)
gen treat1=1

*number of immunizations received at the camp
egen num_rec = anycount(bcg_day dt?_day measels_day), values(1/31)
*total number of immunizations, including those recorded as received outside of camp
egen num_imm=anycount(bcg_day dt?_day measels_day), values(1/31 -555)

*complete immunization indicators
gen complete_rec = (num_rec==5)
gen complete_imm = (num_imm==5)

*Drop the observations that are not kids with at least 1 immunization
keep if child_or_mother==1
keep if num_imm>0

***Keep the kids who were between 1 and 3 years old when the camp in their hamlet ended
*We generate the date of each shot and a boolean for being received in the camp
foreach shot in `shots' {
	gen `shot'_date = mdy(`shot'_month, `shot'_day, `shot'_year)
}

*get first and last dates in logbook
egen first_date = rowmin(*_date)
egen lastshot_date = rowmax(*_date)
*when was the last camp in each location id
bysort locid: egen last_camp = max(lastshot_date)

*How many days passed for this kid from registration to end of camp?
gen age_inc = (last_camp - first_date)

*Age seems to have been recorded at their first visit.  Use this to figure out how old they are at end (in days)
gen age = age_num if age_description==1
replace age = age_num*7 if age_description==2
replace age = age_num*365 if age_description==3

replace age = age + age_inc

*keep those age 1 to 3 years
drop if age<365 | age>365*3

*display data on number of shots
tab num_imm
tab num_rec

***
***Logbook kids in non-spillover hamlets
***

gen rows = "Total Number of Immunizations" if _n==1
replace rows = "Immunizations Received at a Camp" if _n==3
replace rows = "Completely Immunized" if _n==5
replace rows = "Completely Immunized in Camp" if _n==7

gen zztreat1=.
gen zztreat2=.
gen zztreat3=.
gen zzdtreat2=.
gen zzdtreat3=.
gen zzdtreat32=.

gen zzortreat2=.
gen zzortreat3=.
gen zzortreat32=.


local i=1


local varlist "num_imm complete_imm"

foreach var of local varlist {

if (`i'>7) replace rows = "`var'" if _n==`i'

foreach num of numlist 2(1)3 {
reg `var' if treat`num'==1 & spillover==0, cluster(locid)
replace zztreat`num'=_b[_cons] if _n==`i'
replace zztreat`num'=_se[_cons] if _n==`i'+1
}



foreach num of numlist 3 {

xtmixed `var' treat3 if (treat3==1|treat2==1)&spillover==0 || locid:, technique(dfp)
replace zzdtreat32=_b[treat3] if _n==`i'
replace zzdtreat32=_se[treat3] if _n==`i'+1

xtmepoisson `var' treat3 if (treat3==1|treat2==1)&spillover==0 || locid:
replace zzortreat32=exp(_b[treat3]) if _n==`i'
replace zzortreat32=_se[treat3] if _n==`i'+1

}


local i=`i'+2
}


outsheet rows zz* using BMJRR_treattables_2_logbooknospill.csv, comma replace

***
***Logbook kids in spillover hamlets
***
drop zz*
gen zztreat1=.
gen zztreat2=.
gen zztreat3=.
gen zzdtreat2=.
gen zzdtreat3=.
gen zzdtreat32=.

gen zzortreat2=.
gen zzortreat3=.
gen zzortreat32=.

local i=1

local varlist "num_imm complete_imm"


foreach var of local varlist {

if (`i'>7) replace rows = "`var'" if _n==`i'

foreach num of numlist 2(1)3 {
reg `var' if treat`num'==1 & spillover==1, cluster(locid)
replace zztreat`num'=_b[_cons] if _n==`i'
replace zztreat`num'=_se[_cons] if _n==`i'+1
}



foreach num of numlist 3 {

xtmixed `var' treat3 if (treat3==1|treat2==1)&spillover==1 || locid:, technique(dfp)
replace zzdtreat32=_b[treat3] if _n==`i'
replace zzdtreat32=_se[treat3] if _n==`i'+1

xtmepoisson `var' treat3 if (treat3==1|treat2==1)&spillover==1 || locid:
replace zzortreat32=exp(_b[treat3]) if _n==`i'
replace zzortreat32=_se[treat3] if _n==`i'+1
}


local i=`i'+2
}


outsheet rows zz* using BMJRR_treattables_4_logbookspill.csv, comma replace

cap log close












 
 
